package com.fone.player.view;

import java.util.List;

import com.fone.player.R;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class HistorySearchPopWindow {
	private PopupWindow mPopupWindow;
	private List<String> mList;
	private Context mContext;
	private View mView;
	private ListView lv;
	private TextView tv_clear;
	private MyAdapter mAdapter;
	private EditText et;
	private IDeleteListener mListener;
	private int height;
	private static String TAG="HistorySearchPopWindow";

	public void showPop() {
		if (mList.size() == 0) {// 没有历史记录
			return;
		}
		if (mPopupWindow == null) {
			mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, false);
			mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x000000));
			mPopupWindow.setOutsideTouchable(true);
			mPopupWindow.setWidth(ScreenUtil.getScreenWidthPix(mContext));
		}
		if (!mPopupWindow.isShowing()) {
//			int Location[] = new int[2];
//			et.getLocationOnScreen(Location);
			if (mList.size() < 5) {
				height = (ScreenUtil.dp2px(50 * (mList.size())));
			} else {
				height = ScreenUtil.dp2px(50 * 4);
			}
			LayoutParams params = lv.getLayoutParams();
			params.height = height;
			try{
				mPopupWindow.showAsDropDown(et, 0, 10);
			}catch(Exception e){
				L.e(TAG, "showPop", e.getMessage());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	public boolean searchPopWindowisShowing() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			return true;
		} else {
			return false;
		}
	}

	public void dismissSearchPopWindow() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
	}

	public HistorySearchPopWindow(Context context, List<String> list,
			EditText et) {
		this.mList = list;
		this.mContext = context;
		this.et = et;
		mView = LayoutInflater.from(mContext).inflate(
				R.layout.history_pop_window, null);
		initView();
		if (mList.size() > 0) {
			if (mList.size() < 5)
				height = ScreenUtil.dp2px((mList.size()) * 50);
		} else {
			height = ScreenUtil.dp2px(4 * 50);
		}
		lv.getLayoutParams().height = height;
		mPopupWindow = new PopupWindow(mView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, false);
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x000000));
		mPopupWindow.setOutsideTouchable(true);

	}

	private void initView() {
		lv = (ListView) mView.findViewById(R.id.lv);
		tv_clear = (TextView) mView.findViewById(R.id.tv_clear);
		mAdapter = new MyAdapter();
		lv.setAdapter(mAdapter);
		tv_clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mList.clear();
				mAdapter.notifyDataSetChanged();
				if (mListener != null) {
					mListener.deleteAll();
				}
				mPopupWindow.dismiss();
			}
		});
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				L.i(TAG, "OnItemClickListener", "点击item了");
				if (mListener != null) {
					mPopupWindow.dismiss();		
					mListener.selectItem(position);
				}
					
			}
		});
	}

	private class MyAdapter extends BaseAdapter {

//		@Override
//		public boolean isEnabled(int position) {
//			 return false;
//			
//		}
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		ViewHolder viewHolder;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(mContext);
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.history_search_item,
						parent, false);
				viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
				viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.tv.setText(mList.get(position));
			viewHolder.tv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					L.i(TAG, "viewHolder.tv.setOnClickListener", "点击tv了");
					if (mListener != null) {
						mListener.selectItem(position);
					}
					mPopupWindow.dismiss();
				}
			});

			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					L.i(TAG, "convertView.setOnClickListener", "点击convertView了");
					if (mListener != null) {
						mListener.selectItem(position);
					}
					mPopupWindow.dismiss();
				}
			});
			viewHolder.iv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mListener != null) {
						mListener.delete(position);
						mAdapter.notifyDataSetChanged();
						if (mList.size() == 0) {
							mPopupWindow.dismiss();
							mPopupWindow = null;
						} else {
							if (mList.size() < 5) {
								height = ScreenUtil.dp2px((mList.size()) * 50);
							} else {
								height = ScreenUtil.dp2px(4 * 50);
							}
							LayoutParams params = lv.getLayoutParams();
							params.height = height;
							// mPopupWindow.setHeight(height);
						}
					}
				}
			});
			return convertView;
		}

		class ViewHolder {
			public TextView tv;
			public ImageView iv;
		}
	}

	public void setDeleteListener(IDeleteListener listener) {
		this.mListener = listener;
	}

	public interface IDeleteListener {
		void delete(int position);

		/**
		 * 
		 * 选中某条历史记录
		 * 
		 * @param position
		 *            选中条目的position
		 * @return void
		 * @throws
		 */
		void selectItem(int position);

		void deleteAll();
	}
}
